# nr 人名
# ns 地名
# un 未知词
place_dict = ['钱塘江','牛家村','卫州']
# 清理分词

# 词性
import jieba.posseg as psg

tmpstr = ' '.join(word_list)
tmpres = psg.cut(tmpstr) # 附加词性的分词结果

names = []
places = []
for item in tmpres:
    if item.word in place_dict:
        places.append(item.word)
    elif item.flag == 'nr':
        names.append(item.word)
    elif item.flag == 'ns':
        places.append(item.word)
all_words = names + places
# 掩膜
# 把这里背景图设置成其他图
cloudobj = wordcloud.WordCloud(font_path = myfont,
    mask = imageio.imread("射雕背景0.jpg"),
    mode = "RGBA", background_color = None
    ).generate(' '.join(all_words))
# 色系
# 人名设为蓝色，地名设为红色
color_to_words = {
    'blue': set(names),
    'red': set(places)
}
default_color = 'grey' # 指定其他词条的颜色
grouped_color_func = GroupedColorFunc(color_to_words, default_color)
cloudobj.recolor(color_func=grouped_color_func)

plt.imshow(cloudobj)
plt.axis("off")
plt.show()
# 指定分组色系
img1 = imageio.imread("blue.png")
img2 = imageio.imread("green.png")
image_blue = wordcloud.ImageColorGenerator(np.array(img1))
image_green = wordcloud.ImageColorGenerator(np.array(img2))
cloudobj.recolor(color_func=image_blue)
plt.imshow(cloudobj)
plt.axis("off")
plt.show()
cloudobj.recolor(color_func=image_green)
plt.imshow(cloudobj)
plt.axis("off")
plt.show()
# 不同掩膜
# 下面改成不同的图片
from PIL import Image
cloudobj = wordcloud.WordCloud(font_path = myfont,
    mask = np.array(Image.open('mask.jpg')),
    mode = "RGBA", background_color = None
    ).generate(' '.join(all_words))
plt.imshow(cloudobj)
plt.axis("off")
plt.show()
cloudobj = wordcloud.WordCloud(font_path = myfont,
    mask = np.array(Image.open('mask2.jpg')),
    mode = "RGBA", background_color = None
    ).generate(' '.join(all_words))
plt.imshow(cloudobj)
plt.axis("off")
plt.show()